//
//  PreviewWenjuanController.m
//  NPS
//
//  Created by jadd on 15/10/20.
//  Copyright © 2015年 jadd. All rights reserved.
//

#import "PreviewWenjuanController.h"


@interface PreviewWenjuanController()

@end

@implementation PreviewWenjuanController


@synthesize surveyLink;
@synthesize _progressView;
@synthesize _progressProxy;


-(void)backButton{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)setUpView
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom] ;
    button.frame = CGRectMake(0, 0, K_Global_BackButton_Width, K_Global_BackButton_Height);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonClicked:)];
    self.navigationItem.rightBarButtonItem = shareButton;


}

-(void)setUpWebView
{
    CGRect rect = CGRectMake(0, 0, K_ScreenWidth, K_ScreenHeight-64);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:rect];
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
    NSLog(@"disgfudfg-----%@",self.surveyLink);
    //jadd 2015-11-17 显示是新的地址，分享的话还是原来的地址
    NSString *newUrl = [[NSString alloc] initWithFormat:@"%@?test=1",self.surveyLink];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:newUrl]]; // 定义请求地址
    [webView loadRequest:request];
    [self.view addSubview:webView];
}

- (void)viewDidLoad {
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"预览问卷";
    
    [self setUpView];
    
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [NSThread sleepForTimeInterval:1.5];
//        
//    });
    [self setUpWebView];
    [self.navigationController.navigationBar addSubview:_progressView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // Remove progress view
    // because UINavigationBar is shared with other ViewControllers
    [_progressView removeFromSuperview];
}


#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    //self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

//几个代理方法

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSLog(@"webViewDidStartLoad");
    
}

- (void)webViewDidFinishLoad:(UIWebView *)web{
    
    NSLog(@"webViewDidFinishLoad");
    
}

-(void)webView:(UIWebView*)webView  DidFailLoadWithError:(NSError*)error{
    
    NSLog(@"DidFailLoadWithError");
    
}


- (IBAction)shareButtonClicked:(id)sender {
    ShareView *shareView = [ShareView createViewFromNib];
    shareView.surveyLinks = self.surveyLink;
    //[shareView showInWindow];
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:shareView preferredStyle:TYAlertControllerStyleAlert];
    // blur effect
    //[alertController setBlurEffectWithView:self.view];
    //alertController.alertViewOriginY = 60;
    [self presentViewController:alertController animated:YES completion:nil];
//
    

    
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:globalUMengAppKey
//                                      shareText:self.surveyLink
//                                     shareImage:[UIImage imageNamed:@"icon.png"]
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToTencent,UMShareToQQ,nil]
//                                       delegate:nil];
    
}

-(NSArray *)umSocialPlatformArray{
    return @[UMShareToWechatSession,    //微信好友
             UMShareToWechatTimeline,   //微信朋友圈
             UMShareToSina,             //新浪
             UMShareToQQ,               //QQ
             UMShareToQzone,            //QQ空间
             UMShareToSms];             //短信
}
@end
